﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CashOrCard.Helper;
using CashOrCard.Models;
using CashOrCard.Repository;
using CashOrCard.Extensions;

namespace CashOrCard.Controllers
{
    public enum EnumPage
    {
        about,
        business,
        mt,
        joinus
    }
    
    public class SitePageController : Controller
    {
        private readonly IBaseRepository<User> _userRep;
        private readonly IBaseRepository<SitePage> _spRep;
        public SitePageController(IBaseRepository<User> userRep, IBaseRepository<SitePage> spRep)
        {
            this._userRep = userRep;
            this._spRep = spRep;
        }

        [CashOrCardAuthorizeExtension(AllowPermissions = "sitepagemanagement_access", ErrorView = "PermissionDenied")]
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult About()
        {
            var sp = _spRep.LoadEntities(x => x.SitePageTitle.ToLower().Equals(EnumPage.about.ToString())).FirstOrDefault();
            if (sp == null)
            {
                return HttpNotFound();
            }            
            return View("page", sp);
        }

        [HttpGet]
        public ActionResult BusinessModel()
        {
            var sp = _spRep.LoadEntities(x => x.SitePageTitle.ToLower().Equals(EnumPage.business.ToString())).FirstOrDefault();
            if (sp == null)
            {
                return HttpNotFound();
            }
            return View("page", sp);
        }

        [HttpGet]
        public ActionResult MT()
        {
            var sp = _spRep.LoadEntities(x => x.SitePageTitle.ToLower().Equals(EnumPage.mt.ToString())).FirstOrDefault();
            if (sp == null)
            {
                return HttpNotFound();
            }
            return View("page", sp);
        }

        [HttpGet]
        public ActionResult JoinUs()
        {
            var sp = _spRep.LoadEntities(x => x.SitePageTitle.ToLower().Equals(EnumPage.joinus.ToString())).FirstOrDefault();
            if (sp == null)
            {
                return HttpNotFound();
            }
            return View("page", sp);
        }

        public ActionResult Create()
        {
            return View();
        }

        public ActionResult Edit()
        {
            return View();
        }

        [CashOrCardAuthorizeExtension(AllowPermissions = "sitepagemanagement_access", ErrorView = "PermissionDenied")]
        public ActionResult SavePage(SitePage sp)
        {
            sp.CreateDate = DateTime.Now;
            sp.UpdateDate = DateTime.Now;
            sp.IsEnable = true;
            sp.UserId = Common.CurrentUser.UserId;
            bool added = _spRep.AddEntities(sp);
            if (added)
                return Content("success");
            else
                return Content("Some errors occured.");
        }

        [CashOrCardAuthorizeExtension(AllowPermissions = "sitepagemanagement_access", ErrorView = "PermissionDenied")]
        public ActionResult GetAllPages()
        {
            int pageIndex = Request["page"] == null ? 1 : int.Parse(Request["page"]);
            int pageSize = Request["rows"] == null ? 10 : int.Parse(Request["rows"]);
            int total = 0;
            var spList = _spRep.LoadPagerEntities(pageSize, pageIndex, out total, x => x.IsEnable, true, x => x.SitePageId);

            var list = (from sp in spList
                        select new
                        {
                            SitePageId = sp.SitePageId,
                            SitePageTitle = sp.SitePageTitle,
                            SitePageSubTitle = sp.SitePageSubTitle,
                            SitePageBody = sp.SitePageBody,
                            CreateDate = sp.CreateDate,
                            CreateUser = string.Format("{0} {1}", sp.User.FirstName, sp.User.LastName)
                        }).ToList();
            var result = new { total = total, rows = list };
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [CashOrCardAuthorizeExtension(AllowPermissions = "sitepagemanagement_access", ErrorView = "PermissionDenied")]
        public ActionResult DeleteSP(int id)
        {
            var sp = _spRep.Get(id);
            sp.IsEnable = false;
            var result = _spRep.UpdateEntities(sp);
            if (result)
                return Content("success");
            else
                return Content("Some errors occured.");
        }

        [CashOrCardAuthorizeExtension(AllowPermissions = "sitepagemanagement_access", ErrorView = "PermissionDenied")]
        public ActionResult GetSPById(int id)
        {
            var temp = _spRep.Get(id);
            var sp = new SitePage
            {
                SitePageId = temp.SitePageId,
                SitePageTitle = temp.SitePageTitle,
                SitePageSubTitle = temp.SitePageSubTitle,
                SitePageBody = temp.SitePageBody,
                CreateDate = temp.CreateDate
            };
            return Json(sp, JsonRequestBehavior.AllowGet);
        }

        [CashOrCardAuthorizeExtension(AllowPermissions = "sitepagemanagement_access", ErrorView = "PermissionDenied")]
        public ActionResult UpdatePage(SitePage sitepage)
        {
            var sp = _spRep.Get(sitepage.SitePageId);
            sp.SitePageTitle = sitepage.SitePageTitle;
            sp.SitePageSubTitle = sitepage.SitePageSubTitle;
            sp.SitePageBody = sitepage.SitePageBody;
            sp.UpdateDate = DateTime.Now;
            bool updated = _spRep.UpdateEntities(sp);
            if (updated)
                return Content("success");
            else
                return Content("Some errors occured.");
        }

        [CashOrCardAuthorizeExtension(AllowPermissions = "sitepagemanagement_access", ErrorView = "PermissionDenied")]
        public ActionResult GetSPByTitle(string title)
        {
            var temp = _spRep.LoadEntities(x => x.SitePageTitle.ToLower() == title.ToLower()).FirstOrDefault();
            var sp = new SitePage
            {
                SitePageId = temp.SitePageId,
                SitePageTitle = temp.SitePageTitle,
                SitePageSubTitle = temp.SitePageSubTitle,
                SitePageBody = temp.SitePageBody,
                CreateDate = temp.CreateDate
            };
            ViewBag.SitePage = sp;
            return View();
        }
    }
}
